//
//  JCWShareVC.swift
//  HengYouCai
//
//  Created by wll on 2018/1/5.
//  Copyright © 2018年 lwj. All rights reserved.
//  分享弹框界面--代码

import UIKit

class JCWShareVC: BaseViewController {

    //分享配置
    var shareTitle:String = ""
    var shareDescription: String = ""
    var shareUrl:String = ""
    var shareImageUrl:String = ""
    
    
    ///分享结果回调
    public var shareResultHandler: ((_ result: String) ->  Void)?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.createCV()
    }


    private func createCV() {
        self.view.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5)

        self.bottomCancleBtn.frame = CGRect(x: 15, y: JCWSCREENH-45-20.ah_wl, width: JCWSCREENW-30, height: 45)
        self.view.addSubview(self.bottomCancleBtn)
        
        self.view.addSubview(self.bottomContainerView)
        self.bottomContainerView.bounds = CGRect(x: 0, y: 0, width: self.bottomCancleBtn.width_sd, height: 200)
        self.bottomContainerView.bottom_sd = self.bottomCancleBtn.top_sd - 7
        self.bottomContainerView.centerX_sd = self.view.centerX_sd

        //按钮
        let titleArr = ["微信","朋友圈","QQ好友","QQ空间","短信"]
        let imageNamedArr = ["Invite_wechat", "Invite_cof", "Invite_qqfriend",  "Invite_QQZone",  "Invite_message"]
        
        let btnW = self.bottomContainerView.width_sd/4
        let btnH = self.bottomContainerView.height_sd/2
        for i in 0..<5 {
            let btn = UIButton(type: .custom)
            btn.setTitle(titleArr[i], for: .normal)
            btn.setImage(UIImage(named: imageNamedArr[i]), for: .normal)
            btn.frame = CGRect(x: CGFloat(i%4)*btnW, y: CGFloat(i/4)*btnH, width: btnW, height: btnH)
            btn.setTitleColor(Normal_666Color, for: .normal)
            btn.titleLabel?.font = Font_WL(13)
            btn.btnCostomType = .top
            btn.titleLabel?.textAlignment = .center
            btn.tag = 100+i
            btn.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
            self.bottomContainerView.addSubview(btn)
        }
        
    }
    
    //MARK:按钮点击事件
    @objc fileprivate func btnClick(sender: UIButton) {
        switch sender.tag {
        case 100://微信好友
            self.handlerShareContent(.subTypeWechatSession)
            break
        case 101://微信朋友圈
            self.handlerShareContent(.subTypeWechatTimeline)
            break
        case 102://QQ好友
            self.handlerShareContent(.subTypeQQFriend)
            break
        case 103://QQ空间
            self.handlerShareContent(.subTypeQZone)
            break
        case 104://短信
            self.handlerShareContent(.typeSMS)
            break
        default://105取消
            break
        }
        self.dismissVC()
    }
    
    //处理分享内容
    func handlerShareContent(_ shareIndexType: SSDKPlatformType) {
        if self.shareTitle.count == 0{
            //取缓存 否则取默认值
            //let shareUrlStr
            self.shareUrl = WLFileManager.readUserData(forKey: kCusShareLink) as? String ?? ("https://www.ziyijinfu.com/wap/share?signcode=" + (WLFileManager.readUserData(forKey: kCusCode) as? String ?? ""))
                //let shareTitle
            self.shareTitle = WLFileManager.readUserData(forKey: kCusTitle) as? String ?? "子壹财富国资控股，投资更灵活资金更安全 "
            //let shareContent
            self.shareDescription = WLFileManager.readUserData(forKey: kCusDescrption) as? String ?? "专享1000元红包+0.5%加息券+12%年化投资"
            // let shareImageUrl
            self.shareImageUrl = WLFileManager.readUserData(forKey: kCusShareImageUrl) as? String ?? "https://api.ziyijinfu.com/res/website/images/share.png"
        }
        var imageObj:String = ""
        var content = ""
        
        if shareIndexType != .typeSMS ,  self.shareImageUrl.count > 0 {
            imageObj = self.shareImageUrl
        }
        if shareIndexType == .typeSMS {
            content = self.shareDescription + self.shareUrl
        }else{
            content = self.shareDescription
        }
        
        
        //短信分享的时候不要图片
        let shareParams = ShareManagerMob.share().shareContent(withTitle: shareTitle,
                                                               content: content,
                                                               imageObj: imageObj,
                                                               strURL: self.shareUrl,
                                                               mediaType: .auto,
                                                               forPlatformSubType: shareIndexType)
        weak var weakSelf = self
        ShareManagerMob.share().share(with: shareIndexType, parameters: shareParams) { (state, userData, contentEntity, error) in
            if state == .success {
                weakSelf?.shareResultHandler?("分享成功")
            }else if state == .fail {
                weakSelf?.shareResultHandler?("分享失败")
            }
        }
    }
    
    
    //MARK:懒加载
    lazy var bottomCancleBtn: UIButton = {
        let temp = UIButton(type: .custom)
        temp.layer.cornerRadius = 5
        temp.setTitle("取消", for: .normal)
        temp.setTitleColor(Normal_666Color, for: .normal)
        temp.titleLabel?.font = Font_WL(15)
        temp.adjustsImageWhenHighlighted = false
        temp.backgroundColor = UIColor.white
        temp.tag = 105
        temp.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
        return temp
    }()
    
    lazy var bottomContainerView: UIView = {
        let temp = UIView()
        temp.backgroundColor = UIColor.white
        temp.layer.cornerRadius = 5

        return temp
    }()

}
